﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace ADECMS.UserPages
{
    public class AlphanumComparatorFast : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            string s1 = x as string;
            if (s1 == null)
            {
                return 0;
            }
            string s2 = y as string;
            if (s2 == null)
            {
                return 0;
            }

            int len1 = s1.Length;
            int len2 = s2.Length;
            int marker1 = 0;
            int marker2 = 0;

            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = s1[marker1];
                char ch2 = s2[marker2];

                char[] space1 = new char[len1];
                int loc1 = 0;
                char[] space2 = new char[len2];
                int loc2 = 0;

                do
                {
                    space1[loc1++] = ch1;
                    marker1++;

                    if (marker1 < len1)
                    {
                        ch1 = s1[marker1];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

                do
                {
                    space2[loc2++] = ch2;
                    marker2++;

                    if (marker2 < len2)
                    {
                        ch2 = s2[marker2];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

                string str1 = new string(space1);
                string str2 = new string(space2);

                int result;

                if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
                {
                    int thisNumericChunk = int.Parse(str1);
                    int thatNumericChunk = int.Parse(str2);
                    result = thisNumericChunk.CompareTo(thatNumericChunk);
                }
                else
                {
                    result = str1.CompareTo(str2);
                }

                if (result != 0)
                {
                    return result;
                }
            }
            return len1 - len2;
        }
    }
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SlideHandler : IHttpHandler
    {
        static String presentation;
        static string filepath;
        static int nFajlovi;
        static string[] fajlovi;
        public void ProcessRequest(HttpContext context)
        {
            bool sortiraj = false;
            String pres = context.Request.QueryString["prez"];
            String broj = context.Request.QueryString["broj"];
            int brojac = Convert.ToInt32(broj);
            if (pres != presentation)
            {
                presentation = pres;
                filepath = HttpContext.Current.Server.MapPath("/Uploads/Files/" + Path.GetFileNameWithoutExtension(presentation) + "/");
                System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(filepath);
                fajlovi = Directory.GetFiles(filepath);
                nFajlovi = dInfo.GetFiles().GetUpperBound(0) + 1;

                Array.Sort(fajlovi, new AlphanumComparatorFast());
            }
            FileStream fs = File.OpenRead(fajlovi[brojac]);
            byte[] slika = new byte[fs.Length];
            fs.Read(slika, 0, slika.Length);
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(slika);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
